package com.ks.repair.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Joiner;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.dto.OperateBase;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.repair.dao.BRepairBillExMapper;
import com.ks.repair.entity.BRepairBillEx;
import com.ks.repair.entity.BRepairBillTemplate;
import com.ks.repair.dao.BRepairBillTemplateMapper;
import com.ks.repair.service.IBRepairBillTemplateService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ks.repair.service._ServiceBase;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报修-会员填写的报修单关联的扩展字段（纵向） 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-24
 */
@Service
public class BRepairBillTemplateServiceImpl extends _ServiceBase<BRepairBillTemplateMapper, BRepairBillTemplate> implements IBRepairBillTemplateService {

    @Resource
    private  BRepairBillTemplateMapper bRepairBillTemplateMapper;


    @Override
    public Map<String, Object> selectRepairTemplate(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        EntityWrapper<BRepairBillTemplate> wrapper =new EntityWrapper<>();
        BRepairBillTemplate bRepairBillTemplate  =  new BRepairBillTemplate();
        bRepairBillTemplate.setDelMark(Const.DEL_FLAG_NORMAL);
        if(!isSystem(context)){
            bRepairBillTemplate.setOrgID(context.getOrgId().intValue());
            bRepairBillTemplate.setOrgSubID(context.getOrgSubId().intValue());
            bRepairBillTemplate.setTenantID(context.getTenantId().intValue());
        }
        wrapper.setEntity(bRepairBillTemplate);
        wrapper.orderBy("iNumber");
        Page limit = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        List<BRepairBillTemplate> bRepairBillTemplateList = bRepairBillTemplateMapper.selectPage(limit,wrapper);
        Integer count = bRepairBillTemplateMapper.selectCount(wrapper);
        Map<String, Object> resultMap = this.isResultMap(bRepairBillTemplateList, searchCriteriaBase);
        resultMap.put("count", count);
        return resultMap;
    }

    @Override
    public int addRepairTemplate(OperationContext context, BRepairBillTemplate bRepairBillTemplate) {
        if(!isSystem(context)){
            bRepairBillTemplate.setOrgSubID(context.getOrgSubId().intValue());
            bRepairBillTemplate.setOrgID(context.getOrgId().intValue());
            bRepairBillTemplate.setTenantID(context.getTenantId().intValue());
        }
        bRepairBillTemplate.setCreateUserID(context.getUserId().intValue());
        Integer i = bRepairBillTemplateMapper.insert(bRepairBillTemplate);
        return i;
    }

    @Override
    public int updateRepairTemplate(OperationContext context, BRepairBillTemplate bRepairBillTemplate) {
        Integer i = bRepairBillTemplateMapper.updateById(bRepairBillTemplate);
        return i;
    }

    @Override
    public List deleteRepairTemplate(OperationContext context, OperateBase operateBase) {
        ArrayList<String> ids = operateBase.getIdList();
        List newId = new ArrayList<Integer>();
        BRepairBillTemplate bRepairBillTemplate  =  new BRepairBillTemplate();
        bRepairBillTemplate.setDelMark(Const.DEL_FLAG_DEL);
        for(String id:ids){
            bRepairBillTemplate.setId(Integer.valueOf(id));
            Integer i = bRepairBillTemplateMapper.updateById(bRepairBillTemplate);
            if(i>0){
                newId.add(bRepairBillTemplate.getId());
            }
        }
        return newId;
    }

    @Override
    public Map<String, Object> getRepairCustom(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        EntityWrapper<BRepairBillTemplate> wrapper =new EntityWrapper<>();
        BRepairBillTemplate bRepairBillTemplate  =  new BRepairBillTemplate();
        bRepairBillTemplate.setDelMark(Const.DEL_FLAG_NORMAL);
        bRepairBillTemplate.setIEnable("0");
        bRepairBillTemplate.setTenantID(context.getTenantId().intValue());
        wrapper.setEntity(bRepairBillTemplate);
        wrapper.orderBy("iNumber");
        List<BRepairBillTemplate> bRepairBillTemplateList = bRepairBillTemplateMapper.selectList(wrapper);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", bRepairBillTemplateList);
        return resultMap;
    }
}
